<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew ref="searchuinew" :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording" @change="handleChange"></searchuinew>
      </div>
      <div class="panel-container no-margin">
        <p class="line" v-if="searchParams.prov_name">地区：{{ searchParams.prov_name }}</p>
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="序号" type="index" width="100"></el-table-column>
          <el-table-column align="center" header-align="center" label="公司名称" prop="company_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系人" prop="contact_person"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系电话" prop="mobile_phone"></el-table-column>
          <el-table-column align="center" header-align="center" label="会员等级" prop="membership_level"></el-table-column>
          <el-table-column align="center" header-align="center" label="会员开始时间" prop="begin_year"></el-table-column>
          <el-table-column align="center" header-align="center" label="会员结束时间" prop="over_year"></el-table-column>
          <el-table-column align="center" header-align="center" label="年份（年）" prop="years_in_recycling"></el-table-column>
          <el-table-column align="center" header-align="center" label="渠道">
            <template> hss </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" label="地区" prop="address"></el-table-column>
          <el-table-column align="center" header-align="center" label="更新时间" prop="created_time"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="100">
            <template slot-scope="scope">
              <el-button size="mini" type="text" class="grey" @click="handleShop(scope)">店铺</el-button>
              <el-button size="mini" type="text" class="grey" @click="handleDetail(scope)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiCurrentMember, apiMemberClassRank } from '@/api/industryData'
import { formatFormData } from '@/utils'
export default {
  name: 'index',
  data() {
    const vipLevelList = [
      { id: 1, text: '钻石VIP' },
      { id: 2, text: '银钻VIP' },
      { id: 3, text: '金钻VIP' },
      { id: 4, text: '财富通至尊' },
    ]
    return {
      vipLevelList: vipLevelList,
      searchFormArr: [
        {
          fieldName: '企业名称：',
          type: 'input',
          field: 'company_name',
          placeholder: '请输入企业名称',
        },
        {
          fieldName: '联系电话：',
          type: 'input',
          field: 'phone',
          placeholder: '请输入联系电话',
        },
        {
          fieldName: '渠道：',
          type: 'select',
          field: 'source',
          option: [{ id: '回收商', text: 'hss' }],
          placeholder: '请选择渠道',
        },
        {
          fieldName: '当前会员等级：',
          type: 'select',
          field: 'membership_level_code',
          option: vipLevelList,
          placeholder: '请选择会员等级',
        },
        {
          fieldName: '分类：',
          type: 'select',
          field: 'cls',
          option: vipLevelList,
          placeholder: '请选择会员等级',
        },
        {
          fieldName: '会员开始时间',
          type: 'timePicker',
          field: 'time1',
          placeholder: '请选择会员开始时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
        {
          fieldName: '会员结束时间',
          type: 'timePicker',
          field: 'time2',
          placeholder: '请选择会员结束时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
        {
          fieldName: '在回收商年份',
          type: 'select',
          field: 'year_recy_start',
          option: [],
          placeholder: '请选择',
          width: '100',
        },
        {
          fieldName: '至',
          type: 'select',
          field: 'year_recy_end',
          option: [],
          placeholder: '请选择',
          width: '100',
        },
      ],
      isAccording: true,
      searchParams: {
        company_name: '',
        phone: '',
        source: '',
        membership_level_code: '',
        cls: '',
        member_begin_start_time: '',
        member_begin_end_time: '',
        member_over_start_time: '',
        member_over_end_time: '',
        year_recy: '',
        prov_name: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      this.setYearOptions('year_recy_start', 1, 30)
      this.setYearOptions('year_recy_end', 1, 30)
      apiMemberClassRank().then((res) => {
        const options = []
        for (let key in res.data) {
          options.push({
            id: key,
            text: key,
          })
        }
        const searchCls = this.searchFormArr.find((item) => item.field === 'cls')
        searchCls.option = options
      })
      if (this.$route.query.source) {
        const { address, level, source } = this.$route.query
        if (address) {
          this.searchParams.prov_name = address
        }
        if (level) {
          const code = this.vipLevelList.find((item) => item.text === level).id
          this.$refs.searchuinew.clearFormItem('membership_level_code', code)
        }
        if (source) {
          this.$refs.searchuinew.clearFormItem('source', '回收商')
        }
      } else {
        this.getDataList()
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const formData = formatFormData(Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams))
      apiCurrentMember(formData)
        .then((res) => {
          const { items, total } = res.result
          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 回收商年份变化
    handleChange({ item, fieldArr }) {
      if (item.field === 'year_recy_start') {
        if (fieldArr.year_recy_start) {
          this.setYearOptions('year_recy_end', fieldArr.year_recy_start, 30)
        } else {
          this.setYearOptions('year_recy_end', 1, 30)
        }
      } else if (item.field === 'year_recy_end') {
        if (fieldArr.year_recy_end) {
          this.setYearOptions('year_recy_start', 1, fieldArr.year_recy_end)
        } else {
          this.setYearOptions('year_recy_start', 1, 30)
        }
      }
    },
    setYearOptions(field, start, end) {
      const options = []
      for (let i = start; i <= end; i++) {
        options.push({
          id: i,
          text: i,
        })
      }
      const searchStartTime = this.searchFormArr.find((item) => item.field === field)
      searchStartTime.option = options
    },
    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (item.type === 'timePicker' || item.field === 'year_recy_start' || item.field === 'year_recy_end') {
          return
        }
        if (e[item.field] || e[item.field] === 0) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.time1) {
        this.searchParams.member_begin_start_time = e.time1[0]
        this.searchParams.member_begin_end_time = e.time1[1]
      } else {
        this.searchParams.member_begin_start_time = this.searchParams.member_begin_end_time = ''
      }
      if (e.time2) {
        this.searchParams.member_over_start_time = e.time2[0]
        this.searchParams.member_over_end_time = e.time2[1]
      } else {
        this.searchParams.member_over_start_time = this.searchParams.member_over_end_time = ''
      }
      if (e.year_recy_start && e.year_recy_end) {
        this.searchParams.year_recy = e.year_recy_start + '-' + e.year_recy_end
      } else {
        this.searchParams.year_recy = ''
      }

      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 店铺
    handleShop(scrop) {
      window.open(scrop.row.store_link)
    },
    // 详情
    handleDetail(scope) {
      this.$toast('暂未开通')
    },
    prevVIPLevel(code) {
      const item = this.vipLevelList.find((item) => item.id === parseInt(code - 1))
      if (item) {
        return item.text
      } else {
        return '-'
      }
    },
  },
}
</script>

<style scoped lang="scss"></style>
